// miniprogram/pages/editView/personalCenter/recruitmentManagement.js
var requestManager = require('../../../utils/requestManager.js')
var util = require('../../../utils/util.js')
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    recruitmentDataList: [
      
    ],
    nextUrl: '',
    'toolDataList': [
      {
        'image': 'https://extend.qnssl.xuemei99.com/icon_pc_delete.png',
        'text': '删除'
      },
      {
        'image': 'https://extend.qnssl.xuemei99.com/icon_pc_shangjia.png',
        'text': '上架'
      },
      {
        'image': 'https://extend.qnssl.xuemei99.com/icon_pc_edit.png',
        'text': '编辑'
      },
      {
        'image': 'https://extend.qnssl.xuemei99.com/icon_rm_copy.png',
        'text': '复制'
      },
      {
        'image': 'https://extend.qnssl.xuemei99.com/icon_rm_contact.png',
        'text': '联系'
      }
    ]
  },

  /////////////// 公有方法 ///////////////
  /** 
   * 将网络请求活动数据转化成UI需要的数据模型数组
   */
  resArrayConverToObjectList: function (resArray, dataArray) {
    for (var i = 0; i < resArray.length; i++) {
      let dic = resArray[i];
      // let labels = dic['welfare'].split(',')
      let labels = dic['welfare']
      let createTime = util.formatUTCDateByFormatStr(dic["create_time"], 'YYYY-MM-DD')
      // 创建招聘信息
      let item = {
        'id': dic['id'],
        'job': dic['vocation'],
        'salary': dic['money'],
        'label': labels,
        'time': createTime,
        'shelve': dic['shelve']
      }
      dataArray.push(item);
    }
  },
  /////////////// 公有方法 ///////////////

  /////////////// 网络请求 ///////////////
  /** 招聘信息列表 
   * shop_id 店铺id
   */ 
  requestRecruitmentList: function (shopId) {
    var that = this
    requestManager.getRequest('recruitment/info/list/shop', {}, function (res) {
      let status = res.status
      console.log(res.results)

      if (status == 0) {
        let array = res.results
        let arr_item = []
        // for(let i = 0; i < array.length;i++){
        //   arr_item = JSON.parse(array[i].welfare)
        // }
        array.map(function(item){
          item.welfare = JSON.parse(item.welfare)
          return item
        })
        console.log(array)
        var recruitmentArray = []
        // 下页链接
        that.data.nextUrl = res.next
        // 将网络请求活动数据转化成UI需要的数据模型数组
        that.resArrayConverToObjectList(array, recruitmentArray)

        that.setData({
          recruitmentDataList: recruitmentArray
        })
      }
    })
  },

  /** 
   * 删除招聘 
   * id 招聘id
   */
  requestDelegateRecruitment: function (recruitmentId, selectIndex) {
    var that = this
    requestManager.postRequest('recruitment/info/delete?id=' + recruitmentId, {}, function (res) {
      let status = res.status
      if (status == 0) {
        // 删除成功
        wx.showToast({
          title: '删除招聘成功！',
          icon: 'none'
        })
        // 取出本地招聘列表
        var recruitmentDataArray = that.data.recruitmentDataList
        // 删除选中的招聘信息
        recruitmentDataArray.splice(selectIndex, 1)
        that.setData({
          recruitmentDataList: recruitmentDataArray
        })
      }
    })
  },

  /** 
   * 招聘上架、下架
   * id 招聘id
   */
  requestShangJiaOrXiajia: function (recruitmentId, selectIndex) {
    var that = this
    requestManager.postRequest('recruitment/info/shelve?id=' + recruitmentId, {}, function (res) {
      let status = res.status
      if (status == 0) {
        // 操作成功
        let shelve = !(that.data.recruitmentDataList[selectIndex].shelve)
        var title = '下架成功！'
        if (shelve) {
          title = '上架成功！'
        }
        wx.showToast({
          title: title,
          icon: 'none'
        })
        // 设置本地数组的上架或下架状态
        let key = 'recruitmentDataList[' + selectIndex + '].shelve'
        
        console.log('shelve ' + that.data.recruitmentDataList[selectIndex].shelve)
        console.log('shelve ' + shelve)
        that.setData({
          [key]: shelve
        })
      }
    })
  },

  /** 
   * 复制招聘
   * id 招聘id
   */
  requestCopyRecruitment: function (recruitmentId) {
    var that = this
    requestManager.postRequest('recruitment/info/copy?id=' + recruitmentId, {}, function (res) {
      let status = res.status
      if (status == 0) {
        // 复制成功
        wx.showToast({
          title: '复制招聘成功！',
          icon: 'none'
        })
        that.requestRecruitmentList()
      }
    })
  },

  /**
   * 上拉加载请求
   */
  requestNextRecruitmentList: function (nextUrl) {
    var that = this
    console.log('nextUrl' + nextUrl)
    let hasff = 'https://wx.extend.xuemei99.com/'
    let next = nextUrl.substring(hasff.length)

    requestManager.getRequest(next, {}, function (res) {
      let status = res.status

      if (status == 0) {
        let array = res.results
        var recruitmentArray = that.data.recruitmentDataList
        // 下页链接
        that.data.nextUrl = res.next
        // 将网络请求活动数据转化成UI需要的数据模型数组
        that.resArrayConverToObjectList(array, recruitmentArray)

        that.setData({
          recruitmentDataList: recruitmentArray
        })
      }
    })
  },
  /////////////// 网络请求 ///////////////

  /////////////// 事件 ///////////////
  /* 新建活动 */
  addJobTap: function (event) {
    wx.navigateTo({
      url: '/pages/editView/recruit/recruit',
    })
  },

  /* tool tap事件 */
  toolViewTap: function (event) {
    let that = this
    let tooltext = event.currentTarget.dataset.text;
    // index 为招聘对应的index 
    let index = event.currentTarget.dataset.index;
    let id = event.currentTarget.dataset.recruitmentId;

    switch (tooltext) {
      case '删除':
        {
          wx.showModal({
            title: '提示',
            content: '确定删除吗?',
            success:function(res){
              if(res.confirm){
                that.requestDelegateRecruitment(id, index)
              }
            }
          })
          
        }
        break;
      case '下架':
        {
          this.requestShangJiaOrXiajia(id, index)
        }
        break;
      case '上架':
        {
          this.requestShangJiaOrXiajia(id, index) 
        }
        break;
      case '编辑':
        {
          wx.navigateTo({
            url: '/pages/editView/recruit/recruit?recruitment_id=' + id,
          })
        }
        break;
      case '复制':
        {
          this.requestCopyRecruitment(id)
        }
        break;
      case '联系':
        {
          wx.navigateTo({
            url: '/pages/editView/personalCenter/recruitmentContact?recruitment_id=' + id,
          })
        }
        break;
    }
  },
  /////////////// 事件 ///////////////

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 请求招聘列表数据
    console.log('请求招聘列表数据')
    let shopId = app.globalData.shop_id
    this.requestRecruitmentList(shopId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉刷新')
    this.requestRecruitmentList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('上拉加载')
    let tNextUrl = this.data.nextUrl
    if (tNextUrl && tNextUrl.length > 0) {
      this.requestNextRecruitmentList(tNextUrl)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})